<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of project
 *
 * @author truongkx
 */
class category extends CP_Controller{
    //put your code here
    function __construct() {
        parent::__construct('categories');
        $this->load->model('cp/category_model');
    }
    
    function binding($type='CLOTHING'){
        $data = $this->category_model->binding($type);
        if(isset($data['rows']))
            $data['rows']=$this->category_model->buildTreeArray($data['rows']);
		if(isset($data['aaData']))
            $data['aaData']=$this->category_model->buildTreeArray($data['aaData']);
        $R=$data;
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($R));
    }
    
    function gets(){
        $data = $this->category_model->get_by_type();
        $data=$this->category_model->buildTreeArray($data);
        $R["result"] = 1;
        $R["message"]='SUCCESS !';
        $R["data"]=$data;
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($R));
    }
    
    function index(){
        echo $c=$this->load->view('cms/cp/01_category/viewport',$this->assigns,true);
    }
    
    function editpanel($type="Web"){
        $this->assigns->type=$type;
        $data = $this->category_model->binding($type);
        if(isset($data['aaData'])){
            $data['aaData']=$this->category_model->buildTreeArray($data['aaData']);
            $this->assigns->cates=$data['aaData'];
        }
        if(isset($data['rows'])){
            $data['rows']=$this->category_model->buildTreeArray($data['rows']);
            $this->assigns->cates=$data['rows'];
        }
        $Id=(int)$this->input->post('Id');
        if($Id>0)
            $this->assigns->item=$this->category_model->onGet($Id);
        $c=$this->load->view('cms/cp/01_category/editpanel',$this->assigns,true);
        $R["result"] = 1;
        $R["message"]='SUCCESS !';
        $R["htmlreponse"]=$c;
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($R));
    }
    
    function commit(){
        $R["result"] = -1;
        $R["message"] = ascii_to_entities("This function to requires an administrative account.<br/>Please check your authority, and try again.");
        if ($this->privilege->aupr_permission == 777) {
            
            $Params = $this->input->post('Params');
            $Id = (int) $this->input->post('Id');
            if ($Id != 0) {
                $Node=$this->category_model->onGet($Id);
                if($Node){
                    if(isset($Params['Parent'])){
                        $ParentId=(int)$Params['Parent'];
                        if($ParentId==$Id){
                            //node cha bang node code::loi
                            $R["result"] = -206;
                            $R["message"] = "[206] Data invalid !";
                            $this->output->set_header('Content-type: application/json');
                            $this->output->set_output(json_encode($R));
                            return;
                        }elseif($ParentId>0){
                            $ParentNode=$this->category_model->onGet($ParentId);
                            if($ParentNode){
                                if($Node->Type!=$ParentNode->Type){
                                    //2 node khác loai::loi
                                    $R["result"] = -205;
                                    $R["message"] = "[205] Node ko cùng loại.";
                                    $this->output->set_header('Content-type: application/json');
                                    $this->output->set_output(json_encode($R));
                                    return;
                                }else{
                                    if(!$this->checknode($Id,$ParentId,$Node->Type)){
                                        //Cau truc loi ::loi
                                        $R["result"] = -207;
                                        $R["message"] = "[207] Data invalid.";
                                        $this->output->set_header('Content-type: application/json');
                                        $this->output->set_output(json_encode($R));
                                        return;
                                    }
                                }
                            }else{
                                //node cha khong ton tai::loi
                                $R["result"] = -204;
                                $R["message"] = "[204] Parent Node doesn't exist.";
                                $this->output->set_header('Content-type: application/json');
                                $this->output->set_output(json_encode($R));
                                return;
                            }
                        }
                    }
                }else{
                    $R["result"] = -203;
                    $R["message"] = "[203] Node doesn't exist.";
                    $this->output->set_header('Content-type: application/json');
                    $this->output->set_output(json_encode($R));
                    return;
                }
                $rs = $this->category_model->onUpdate($Id, $Params);
            } else {
                $rs = $this->category_model->onInsert($Params);
            }
            if ($rs === true) {
                $R["result"] = 1;
                $R["message"] = ascii_to_entities("Success. Data have been save.");
            } else {
                $R["result"] = -1;
                $R["error_number"] = $this->db->_error_number();
                $R["error_message"] = $this->db->_error_message();
                //$R["last_query"] = $this->db->last_query();
                $R["message"] = ascii_to_entities("System rejected your data.<br/>Please check data input and try again.<br/>");
                if ($R["error_number"] != 0){
                    $R["message"].=ascii_to_entities("<span class='erc'>[" . $R["error_number"] . "] " . $R["error_message"] . ".</span>");
                }
                $R['p']=$Params;
            }
        }
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($R));
    }
    
    private function checknode($nodeid,$pnodeid,$type){
        if($nodeid==$pnodeid) return false;
        $data = $this->category_model->get_by_type($type);
        $nodes=$this->category_model->buildTreeArray($data);
        $checking=false;
        $R["message"]='SUCCESS !';
        foreach ($nodes as $node){
            if($checking){
                if($node->Level>$nodelevel){
                    if($node->Id==$pnodeid){
                        return false;
                        break;
                    }
                }else{
                   $checking=false;
                   break;
                }
            }
            if($node->Id==$nodeid){
                $checking=true;
                $nodelevel=$node->Level;
            }
        }
        return true;
    }
            
    function delete() {
        $R["result"] = -1;
        $R["message"] = ("This function to requires an administrative account.<br/>Please check your authority, and try again.");
        if ($this->privilege->aupr_permission == 777) {
            $id = (int) $this->input->post('Id');
            if ($id > 0) {
                $tmp = $this->category_model->onGet($id);
                if ($tmp) {
                    $this->category_model->db->trans_begin();
                    $rs = $this->category_model->onDelete($id);
                    $this->category_model->update_node_by_parent($tmp->Id,$tmp->Parent);
                    $R["q2"]=$this->db->last_query();
                    if($this->category_model->db->trans_status() === FALSE || $rs === false){
                        $this->category_model->db->trans_rollback();
                        $R["result"] = -1;
                        $R["error_number"] = $this->db->_error_number();
                        $R["error_message"] = $this->db->_error_message();
                        $R["message"] = ("Fail. Please check data input and try again.<br/>");
                        if ($R["error_number"] != 0)
                            $R["message"].=("<span class='erc'>[" . $R["error_number"] . "] " . $R["error_message"] . ".</span>");
                    }else{
                        $this->category_model->db->trans_commit();
                        $R["result"] = 1;
                        $R["message"] = ("Success. Record have been deleted.");
                    }
                }else {
                    $R["message"] = ("Record does't exist.");
                }
            } else {
                $R["message"] = ("Id invalid.");
            }
        }
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($R));
    }
    
}

?>
